<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class House extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;
    protected $table = 'house';
    protected $guarded = [];

    public function intermediary()
    {
        return $this->hasOne(IntermediaryModel::class, 'id', 'intermediary_id');
    }

    public function houseAttr()
    {
        return $this->belongsTo(HouseAttr::class, 'house_room', 'id');
    }

    public function setHouseConfigAttribute($value)
    {
        $this->attributes['house_config'] = implode(',',$value);
    }

    public function setHouseLabelAttribute($value)
    {
        $this->attributes['house_label'] = implode(',',$value);
    }

    public static function change($id,$data){
        return self::where('id',$id)->update(['intermediary_id'=>$data]);
    }

    public function direction()
    {
        return $this->hasOne(HouseAttr::class, 'id', 'house_direction');
    }

    //住房类型
    public function propertyType()
    {
        return $this->hasOne(HouseAttr::class, 'id', 'house_property_type');
    }

    //几室几厅
    public function houseRoom()
    {
        return $this->hasOne(HouseAttr::class, 'id', 'house_room');
    }

    //建筑年代
    public function houseYear()
    {
        return $this->hasOne(HouseAttr::class, 'id', 'house_year');
    }

    public function city()
    {
        return $this->hasOne(City::class, 'id', 'house_region');
    }

    public static function getHotHouses()
    {
        return self::with('houseAttr')->where(['house_status' => 1, 'is_recommend' => 1])->limit(6)->get();
    }

    public static function getHouseData($where)
    {
        return self::with('houseAttr')->where($where)->get();
    }

    public static function houseScope($between,$houses_name,$where)
    {
        return self::whereBetween('pay_money', $between)
            ->where('house_area','like',"%$houses_name%")
            ->where($where)
            ->get();
    }
}
